package com.cookor.poi.modules.phy.web;

import com.cookor.poi.common.config.Global;
import com.cookor.poi.common.persistence.Page;
import com.cookor.poi.common.utils.StringUtils;
import com.cookor.poi.common.web.BaseController;
import com.cookor.poi.modules.phy.entity.TTempNotice;
import com.cookor.poi.modules.phy.service.TTempNoticeService;
import com.cookor.poi.modules.sys.utils.UserUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
@RequestMapping({"${adminPath}/phy/tTempNotice"})
public class TTempNoticeController extends BaseController {
  @Autowired
  private TTempNoticeService tTempNoticeService;
  
  @ModelAttribute
  public TTempNotice get(@RequestParam(required = false) String id) {
    TTempNotice entity = null;
    if (StringUtils.isNotBlank(id))
      entity = this.tTempNoticeService.get(id); 
    if (entity == null)
      entity = new TTempNotice(); 
    return entity;
  }
  
  @RequiresPermissions({"phy:tTempNotice:view"})
  @RequestMapping({"list", ""})
  public String list(TTempNotice tTempNotice, HttpServletRequest request, HttpServletResponse response, Model model) {
    tTempNotice.setTenantId(UserUtils.getTenantId());
    Page<TTempNotice> page = this.tTempNoticeService.findPage(new Page(request, response), tTempNotice);
    model.addAttribute("page", page);
    model.addAttribute("tTempNotice", tTempNotice);
    return "modules/phy/tTempNoticeList";
  }
  
  @RequiresPermissions({"phy:tTempNotice:view"})
  @RequestMapping({"form"})
  public String form(TTempNotice tTempNotice, Model model) {
    model.addAttribute("admin", UserUtils.getUser());
    model.addAttribute("tTempNotice", tTempNotice);
    return "modules/phy/tTempNoticeForm";
  }
  
  @RequiresPermissions({"phy:tTempNotice:edit"})
  @RequestMapping({"save"})
  public String save(TTempNotice tTempNotice, Model model, RedirectAttributes redirectAttributes) throws Exception {
    if (!beanValidator(model, tTempNotice, new Class[0]))
      return form(tTempNotice, model); 
    this.tTempNoticeService.mySave(tTempNotice);
    addMessage(redirectAttributes, new String[] { "保存模板消息成功" });
    return "redirect:" + Global.getAdminPath() + "/phy/tTempNotice/?repage";
  }
  
  @RequiresPermissions({"phy:tTempNotice:edit"})
  @RequestMapping({"delete"})
  public String delete(TTempNotice tTempNotice, RedirectAttributes redirectAttributes) {
    this.tTempNoticeService.delete(tTempNotice);
    addMessage(redirectAttributes, new String[] { "删除模板消息成功" });
    return "redirect:" + Global.getAdminPath() + "/phy/tTempNotice/?repage";
  }
}
